package com.oracle.lingshangejia.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.ServicePackageAdapter;
import com.oracle.lingshangejia.bean.DataBean;
import com.oracle.lingshangejia.bean.ServicePackageBean;
import com.oracle.lingshangejia.mvp.base.BaseActivity;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

//服务制定包
public class ServicePackageActivity extends BaseActivity implements BaseQuickAdapter.OnItemChildClickListener {


    @BindView(R.id.rv)
    RecyclerView mRv;
    @BindView(R.id.srl)
    SmartRefreshLayout mSrl;
    private List<ServicePackageBean.DataBean> mDataBeanList;
    private ServicePackageAdapter mAdapter;
    private int mId;


    @Override
    public int setLayoutId() {
        return R.layout.activity_service_package;
    }

    @Override
    public void initData() {
        super.initData();
        showLoading();
        ButterKnife.bind(this);
        mId = getIntent().getIntExtra("id",0);//居民id
        initRecyclerVeiw();
        refresh();
        queryUserServicePackage();
    }


    private void initRecyclerVeiw(){
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ServicePackageAdapter(mDataBeanList);
        mRv.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(this);
    }

    private void refresh(){
        mSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                       queryUserServicePackage();
            }
        });
    }
      //查询居民服务包
    private void queryUserServicePackage(){
        int ysid= (int) SpUtils.get(this,"ysid",0);
        HashMap<Object,Object> map = new HashMap<>();
        map.put("userId",ysid);//当前登录用户id
        map.put("grId",mId );//居民id
        OkHttpUtils.getNetWork(this, Constant.queryServicePackage,map ,handler );
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){
                case 1:
                    dismissLoading();
                    Gson gson = new Gson();
                    String json = (String)msg.obj;
                    ServicePackageBean bean = gson.fromJson(json, ServicePackageBean.class);
                    if (bean.getCode() == 0){
                        mDataBeanList = bean.getData();
                        if (mDataBeanList.size() >0){
                           mAdapter.replaceData(mDataBeanList);
                           mSrl.finishRefresh();
                        }else {
                            MyToast.show("没有服务包");
                        }
                    }else {
                        MyToast.show(bean.getMsg());
                    }


                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(ServicePackageActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(ServicePackageActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(ServicePackageActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()){
            case R.id.tv_name:
            case R.id.tv_date:
                if (mDataBeanList.get(position).getStatus() == 1) {
                    MyToast.show("已完成");
                } else {
                    Intent intent = new Intent(this, ServicePackageDetailActivity.class);
                    intent.putExtra("id",mDataBeanList.get(position).getId() );
                    startActivity(intent);
                }

                break;
            case R.id.tv_finish:
                showDialog(mDataBeanList.get(position));
                break;
        }
    }

    private void showDialog(ServicePackageBean.DataBean dataBean){
        final AlertDialog.Builder normalDialog =
            new AlertDialog.Builder(this);
                normalDialog.setTitle(dataBean.getName());
                normalDialog.setMessage("您是否要结束服务包");
                normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    HashMap<Object,Object> map = new HashMap<>();
                    map.put("dzbId",dataBean.getId() );
                    OkHttpUtils.getNetWork(ServicePackageActivity.this, Constant.finishdServicePackage,map ,finishhandler );
                }
            });
                normalDialog.setNegativeButton("取消",
                 new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                }
            });

        normalDialog.show();
    }



    @SuppressLint("HandlerLeak")
    private Handler finishhandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){
                case 1:
                    Gson gson = new Gson();
                    String json = (String)msg.obj;
                    DataBean dataBean = gson.fromJson(json,DataBean.class );
                    if (dataBean.getCode() == 0){
                        queryUserServicePackage();
                        MyToast.show("结束服务包成功");
                    }else {
                        MyToast.show("结束失败 请重试");
                    }

                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(ServicePackageActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(ServicePackageActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(ServicePackageActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}
